﻿using RN;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CAD.DAO
{
    public class RecordDAOSQLSERVER: RecordDAO
    {
        public override Message getMessage(int messageId)
        {
            CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter tableadapter = new CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter();
            CAD.DS.DSMail.rpt_ListMailsDataTable datatable = tableadapter.ListMailById(messageId);
            if (datatable != null)
            {
                Message m = RowToMessage(datatable[0]);
                return m;
            }
            return null;
        }
        public override bool insertMessage(Message m)
        {
            CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter tableadapter = new CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter();
            if (m != null){
                int i = 0;
                i = tableadapter.Mail_Insert(m.datoscabeceramensaje().From(), 
                                         m.datoscabeceramensaje().To(), 
                                         m.datoscuerpomensaje().Subject(), 
                                         m.datoscuerpomensaje().Messages(), 
                                         m.Estado);
                return true;
            }
            return false;
        }
        public override Client validateUser(Client client)
        {
            CAD.DS.DSClientTableAdapters.Clients_ValidateTableAdapter tableadapter = new CAD.DS.DSClientTableAdapters.Clients_ValidateTableAdapter();
            if (client != null)
            {
                CAD.DS.DSClient.Clients_ValidateDataTable datatable = tableadapter.ClientValidate(client.username, client.password);
                if (datatable != null && datatable.Count > 0)
                {
                    Client c = RowToClient(datatable[0]);
                    return c;
                }

                return null;
                
                //return (datatable != null && datatable.Count > 0) ? RowToClient(datatable[0]) : null;
            }           
            return null;
        }
        public override bool deleteMessage(int id)
        {
            CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter tableadapter = new CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter();
            if (id != 0)
            {
                int i = 0;
                i = tableadapter.Mail_Delete(id);
                return true;
            }

            return false;
        }
        public override bool restoreMessage(int id)
        {
            CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter tableadapter = new CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter();
            if (id != 0)
            {
                int i = 0;
                i = tableadapter.Mail_Restore(id);
                return true;
            }

            return false;
        }
        public override bool updateMessageAsRead(int id)
        {
            CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter tableadapter = new CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter();
            if (id != 0)
            {
                int i = 0;
                i = tableadapter.Mail_IsRead(id);
                return true;
            }

            return false;
        }
        private Message RowToMessage(CAD.DS.DSMail.rpt_ListMailsRow row)
        {
            Message m = new Message();
            m.Id = row.id;
            m.datoscabeceramensaje(row.From, row.To);
            m.datoscuerpomensaje(row.Subject, row.Content);
            m.Estado = row.Estado;
            m.IsRead = (row.IsRead != 0)? true: false;
            return m;
        }
        private Client RowToClient(CAD.DS.DSClient.Clients_ValidateRow row)
        {
            Client cl = new Client();
            cl.username = row.username;
            cl.password = row.password;
            cl.estado = row.Estado;
            return cl;
        }
    }
}
